package com.example.q_kang.hhh.home;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.example.q_kang.hhh.R;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.base.MVPFragment;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.base.App;
import com.sskj.base.bean.TeamCommonData;
import com.sskj.base.mvchelper.ModelRx2DataSource;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.DividerLineItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author Hey
 * Create at  2019/04/09
 */
public class TeamManagerFragment extends MVPFragment<TeamManagerPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.tv_zhitui)
    TextView tvZhitui;
    @BindView(R.id.tv_team)
    TextView tvTeam;
    @BindView(R.id.tv_huiyuan)
    TextView tvHuiyuan;
    Unbinder unbinder;
    private SlimAdapter<TeamCommonData.TeamData> slimAdapter;
    private MVCCoolHelper<List<TeamCommonData.TeamData>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_team_manager;
    }

    @Override
    public TeamManagerPresenter getPresenter() {
        return new TeamManagerPresenter();
    }

    @Override
    public void initView() {
        mPresenter.getTeamCommonData();

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setLeftPadding(30)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setRightPadding(30)
                .setDividerColor(ContextCompat.getColor(getActivity(), R.color.divider_s)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_team_manager, new SlimInjector<TeamCommonData.TeamData>() {
            @Override
            public void onInject(TeamCommonData.TeamData data, IViewInjector injector) {
                injector.text(R.id.tv_account, data.getAccount())
                        .text(R.id.tv_level, data.getPd_lv_name())
                        .text(R.id.tv_paidan, data.getLever_name())
                        .text(R.id.tv_date, DateUtil.getDateFormat(Long.valueOf(data.getReg_time()) * 1000, DateUtil.PATTERN_J))
                        .text(R.id.tv_total_buy, data.getCount_money());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<TeamCommonData.TeamData> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<TeamCommonData.TeamData>)
                        page -> mPresenter.getTeamList(page), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();

    }


    @Override
    public void initData() {

    }

    public static TeamManagerFragment newInstance() {
        TeamManagerFragment fragment = new TeamManagerFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void getCommonDataSuccess(TeamCommonData teamCommonData) {
        tvZhitui.setText(teamCommonData.getCountTjr());
        tvTeam.setText(teamCommonData.getCountDuan());
        tvHuiyuan.setText(teamCommonData.getTjrNumV());
    }
}
